// pages/menu/advertise/advertise.js
var app = getApp()
var verify = require('../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    inputShowed: true,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })
  },

  // 获取广告模板
  getPriceList() {
    var size = this.data.size
    var filterValue = this.data.filterValue
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this

    wx.request({
      url: baseUrl + 'commodity_price2?productModel=ALL&status=ALL&enableTimeBegin=&enableTimeEnd=&filter=commodityPriceName&page=0&size=' + size + '&filterValue=' + filterValue,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("priceList = ",res.data.data)
          var priceList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          that.setData({
            priceList: priceList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 搜索相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 取消搜索
  hideInput: function () {
    this.setData({
      size: 10,
      loading: true,
      filterValue: "",
      isFresh: false,
      inputShowed: false,
    });
    this.getPriceList();
  },
  inputTyping: function (e) {
    this.setData({
      filterValue: e.detail.value
    });
    this.getPriceList();
  },
  // 搜索框输入
  input: function (res) {
    this.setData({
      filterValue: res.detail.value
    })
  },

  // 查看价格详情
  priceDetail(res) {
    var index = res.currentTarget.dataset.index;
    var price = this.data.priceList[index]
    var commodityPriceId = price.commodityPriceId
    var productId = price.productId
    var commodityPriceName = price.commodityPriceName
    wx.navigateTo({
      url: 'price-detail/price-detail?commodityPriceId=' + commodityPriceId 
          + '&productId=' + productId 
          + '&commodityPriceName=' + commodityPriceName 
    })

  },

  // 绑定设备
  bindDevice(res) {
    var index = res.currentTarget.dataset.index
    var commodityPriceId = this.data.priceList[index].commodityPriceId
    var productId = this.data.priceList[index].productId
    // console.log("index = ", res)
    wx.navigateTo({
      url: 'bind-device/bind-device?commodityPriceId=' + commodityPriceId + '&productId=' + productId,
    })
  },

  // 发布
  publish(res) {
    var index = res.currentTarget.dataset.index
    var commodityPriceId = this.data.priceList[index].commodityPriceId
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this

    wx.request({
      url: baseUrl + 'deploy_commodity_price/' + commodityPriceId,
      method:"put",
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '发布成功!',
            duration: 3500
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    this.getPriceList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.getPriceList()
    }
  }
})